package com.ljm.sys.auth.controller;

import com.ljm.common.dto.BaseResult;
import com.ljm.common.util.ResultUtil;
import com.ljm.shiro.util.UserUtil;
import com.ljm.sys.auth.api.SysAuthPermissionApi;
import com.ljm.sys.auth.dto.SysAuthPermissionDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 系统权限 前端控制器
 * </p>
 *
 * @author HuamingChen
 * @since 2018-12-19
 */
@Api(description = "系统权限")
@Controller
@RequestMapping("/auth/sysPermission")
public class SysAuthPermissionController {

    /**
     * 系统权限接口
     */
    @Resource
    private SysAuthPermissionApi sysPermissionApi;
    @ApiOperation(value = "保存系统权限")
    @PostMapping(value = "/save")
    @ResponseBody
    @RequiresPermissions(value={"sys:auth:permission:add","sys:auth:permission:edit"},logical = Logical.OR)
    public BaseResult save(@ApiParam(value = "系统权限") @RequestBody SysAuthPermissionDto dto) {
        sysPermissionApi.saveData(UserUtil.getUserInfo(),dto);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除系统权限")
    @PostMapping(value = "/del")
    @ResponseBody
    @RequiresPermissions(value={"sys:auth:permission:del"})
    public BaseResult del(@ApiParam(value = "主键") @RequestBody List<Long> ids) {
        sysPermissionApi.del(ids);
        return ResultUtil.success();
    }

    @ApiOperation(value = "获取系统权限")
    @GetMapping(value = "/getById")
    @ResponseBody
    @RequiresPermissions(value={"sys:auth:permission:add","sys:auth:permission:edit"},logical = Logical.OR)
    public BaseResult getById(@ApiParam(value = "主键") @RequestParam Long id) {
        return ResultUtil.successData(sysPermissionApi.getDataById(id));
    }

    @ApiOperation(value = "分页获取系统权限")
    @GetMapping(value = "/getByPage")
    @ResponseBody
    @RequiresPermissions(value={"sys:auth:permission:list"})
    public BaseResult getByPage(
            @ApiParam(value = "权限名称") @RequestParam(required = false) String permissionName,
            @ApiParam(value = "权限代码") @RequestParam(required = false) String permissionCode,
            @ApiParam(value = "权限分组") @RequestParam(required = false) String groupName,
            @ApiParam(value = "页码") @RequestParam Integer pageNo,
                                @ApiParam(value = "页大小") @RequestParam Integer pageSize) {
        return ResultUtil.successData(sysPermissionApi.getDataByPage(permissionName,permissionCode,groupName,pageNo,pageSize));
    }

    @GetMapping(value = "/getPermissionsWithGroup")
    @ApiOperation(value="获取所有权限并分组")
    @ResponseBody
    @RequiresPermissions(value={"sys:auth:permission:add","sys:auth:permission:edit"},logical = Logical.OR)
    public BaseResult getPermissionsWithGroup(){
        return ResultUtil.successData(sysPermissionApi.getPermissionsWithGroup());
    }

    @GetMapping(value = "/getAllPermission")
    @ApiOperation(value="获取所有权限")
    @ResponseBody
    @RequiresPermissions(value={"sys:auth:permission:add","sys:auth:permission:edit"},logical = Logical.OR)
    public BaseResult getAllPermission(){
        return ResultUtil.successData(sysPermissionApi.getAllPermission());
    }

}
